package com.fage.manager;

import javax.sql.DataSource;

/**
 * @author lin
 * @version 1.0
 * @date 2020-08-11 14:45
 * @Description TODO
 */
public class TransactionManager {

    private DataSource connectionUtils;

    public void setConnectionUtils(DataSource connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    /**
     * 开启事务
     */
    public  void beginTransaction(){
        try {
            connectionUtils.getConnection().setAutoCommit(false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 提交事务
     */
    public  void commit(){
        try {
            connectionUtils.getConnection().commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 回滚事务
     */
    public  void rollback(){
        try {
            connectionUtils.getConnection().rollback();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 释放连接
     */
    public  void release(){
        try {
            connectionUtils.getConnection().close();//还回连接池中
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
